<template>
  <div class="box">
    <div class="jp-box item" v-if="this.bookData">
      <div class="zc-top">
        <router-link to="/newlist">
          <span>精品推荐</span>
        </router-link>
        <span @click="getBook" class="top-up">
          换一批
          <em class="el-icon-refresh"></em>
        </span>
      </div>
      <div>
        <hr class="line" />
      </div>
      <div
        class="list-item"
        v-for="(bitems, index) in this.bookData"
        :key="bitems.lid + index"
      >
        <router-link :to="`/bookdetails/${bitems.bid}`" class="left">
          <img :src="bitems.bsrc" alt="" />
        </router-link>
        <div class="right">
          <div class="r-top">
            <router-link :to="`/bookdetails/${bitems.bid}`" class="bookname">
              {{ bitems.bname }}
            </router-link>
          </div>
          <span class="word"> 推荐语:{{ bitems.bwords }} </span>
          <span class="user">
            创建人:{{ bitems.name }}
            <em
              @click="addGz(bitems.id)"
              v-once-click
              class="el-icon-message-solid"
            ></em>
          </span>
        </div>
      </div>
    </div>
    <div class="zc-box item" v-if="this.listData">
      <div class="zc-top">
        <router-link to="/booklist">
          <span>珍藏书单</span>
        </router-link>
        <span @click="getList" class="top-up">
          换一批
          <em class="el-icon-refresh"></em>
        </span>
      </div>
      <div>
        <hr class="line" />
      </div>
      <div class="list-item" v-for="items in this.listData" :key="items.lid">
        <router-link :to="`/BooksDetails/${items.lid}`" class="left">
          <img :src="items.lsrc" alt="" />
        </router-link>
        <div class="right">
          <div class="r-top">
            <router-link :to="`/BooksDetails/${items.lid}`" class="bookname">
              {{ items.lname }}
            </router-link>
          </div>
          <span class="word"> 推荐语:{{ items.lwords }} </span>
          <span class="user">
            创建人:{{ items.name }}
            <em
              @click="addGz(items.id)"
              v-once-click
              class="el-icon-message-solid"
            ></em>
          </span>
        </div>
      </div>
    </div>
    <div class="zx-box item" v-if="this.wordData">
      <div class="zc-top">
        <router-link to="/booklist">
          <span>佳句赏析</span>
        </router-link>
        <span @click="getWord" class="top-up">
          换一批
          <em class="el-icon-refresh"></em>
        </span>
      </div>
      <div>
        <hr class="line" />
      </div>
      <div class="list" v-for="(witems, index) in this.wordData" :key="index">
        <div class="right">
          <router-link :to="`/wordsdetails/` + witems.wid">
            <p class="item">{{ witems.words }}</p>
          </router-link>
          <span class="item">
            ——{{ witems.author }}:
            <router-link to="/bookdetails">{{ witems.bname }}</router-link>
          </span>
          <div class="item bottom">
            <div class="bottom-left">
              <h3>发布人:{{ witems.name }}</h3>
              <em
                @click="addGz(witems.id)"
                v-once-click
                class="el-icon-message-solid"
              ></em>
            </div>
            <span class="two">
              <em
                @click="addDz(witems.id, witems.wid)"
                v-once-click
                class="el-icon-trophy"
                >{{ witems.dianzan }}</em
              >
              <em class="el-icon-chat-dot-round">{{ witems.pinglun }}</em>
              <em class="el-icon-star-off">{{ witems.shoucang }}</em>
            </span>
          </div>
        </div>
      </div>
    </div>
    <div class="jx-box item">
      <div class="zc-top">
        <router-link to="/wechat">
          <span>畅所欲言</span>
        </router-link>
        <span @click="getChat" class="top-up">
          换一批
          <em class="el-icon-refresh"></em>
        </span>
      </div>
      <div>
        <hr class="line" />
      </div>
      <div class="jx-inner" v-for="items in this.chatData" :key="items.wclid">
        <div class="talk-item">
          <router-link :to="`/chatdetails/` + items.wcid">
            {{ items.theme }}
          </router-link>
          <em class="el-icon-s-custom">{{ items.name }}</em>
        </div>
        <div class="talk-item">
          <span>
            最新回复:
            <span>{{ items.reply }}</span>
          </span>
          <span>
            {{ items.rname }}
            <em class="el-icon-chat-square"></em>
          </span>
        </div>
        <hr />
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      bookData: null,
      listData: null,
      wordData: null,
      chatData: null,
      num: 0,
    };
  },
  mounted() {
    this.getList();
    this.getBook();
    this.getWord();
    this.getChat();
  },
  methods: {
    // 挂载时(页面出生,显示),自动触发请求
    // 发送请求
    getBook() {
      const url = "http://127.0.0.1:3000/v1/library/book";
      console.log(url);
      this.axios.get(url).then((res) => {
        this.bookData = res.data;
        console.log("this.bookData:", this.bookData);
        console.log(this.bookData);
        console.log(this.bookData.length);
        this.num = Math.floor(Math.random() * (this.bookData.length - 6));
        console.log(this.num);
        this.bookData = this.bookData.slice(this.num, this.num + 5);
        console.log("this.bookData:", this.bookData);
      });
    },
    // 挂载时(页面出生,显示),自动触发请求
    // 发送请求
    getList() {
      const url = "http://127.0.0.1:3000/v1/library/booklist";
      console.log(url);
      this.axios.get(url).then((res) => {
        this.listData = res.data;
        console.log("this.listData:", this.listData);
        console.log("this.listData.length:", this.listData.length);
        this.num = Math.floor(Math.random() * (this.listData.length - 6));
        console.log(this.num);
        this.listData = this.listData.slice(this.num, this.num + 5);
        console.log("this.listData:", this.listData);
      });
    },
    // 挂载时(页面出生,显示),自动触发请求
    // 发送请求
    getWord() {
      const url = "http://127.0.0.1:3000/v1/library/words";
      console.log(url);
      this.axios.get(url).then((res) => {
        this.wordData = res.data;
        console.log("this.wordData:", this.wordData);
        console.log("this.length:", this.wordData.length);
        this.num = Math.floor(Math.random() * (this.wordData.length - 6));
        console.log(this.num);
        this.wordData = this.wordData.slice(this.num, this.num + 5);
        console.log("this.wordData:", this.wordData);
        console.log("this.wordData.length:", this.wordData.length);
      });
    },
    getChat() {
      const url = "http://127.0.0.1:3000/v1/library/wechat";
      console.log(url);
      this.axios.get(url).then((res) => {
        this.chatData = res.data;
        console.log("this.chatData:", this.chatData);
        this.num = Math.floor(Math.random() * (this.chatData.length - 6));
        console.log(this.num);
        this.chatData = this.chatData.slice(this.num, this.num + 5);
        console.log("this.chatData:", this.chatData);
        console.log("this.chatData.length:", this.chatData.length);
      });
    },
    addGz(id) {
      const url4 = "http://127.0.0.1:3000/v1/library/updateguanzhu/";
      const params = `id=${id}`;
      console.log(url4, params);
      this.axios.put(url4, params).then((res) => {
        console.log("关注成功！");
      });
    },
    addDz(id, wid) {
      const url4 = "http://127.0.0.1:3000/v1/library/bestwords/updatedianzan";
      const params = `id=${id}&wid=${wid}`;
      console.log(url4, params);
      this.axios.put(url4, params).then((res) => {
        console.log("点赞成功！");
      });
    },
  },
};
</script>

<style lang="scss" scoped>
* {
  margin: 0;
  padding: 0;
  user-select: none;
}
ul,
ol {
  list-style: none;
}
*a {
  text-decoration: none;
  color: #000;
}

.box {
  width: 1200px;
  margin: 0 auto;
  .item {
    a {
      span {
        display: flex;
        font-size: 25px;
        margin: 15px 15px;
      }
    }

    .line {
      height: 5px;
      background-color: #333;
    }
  }
}
.list-item {
  padding: 15px;
  display: flex;
  justify-content: space-between;
  border-bottom: 1px solid black;

  .left {
    img {
      width: 127px;
      height: 180px;
    }
  }
  .right {
    width: 100%;
    display: flex;
    flex-direction: column;
    margin-left: 15px;
    justify-content: space-around;

    .r-top {
      display: flex;
      justify-content: space-between;
      align-items: center;

      span {
        margin: 10px;
      }
    }
    .bookname {
      display: flex;
      justify-content: flex-start;
      margin-top: 15px;
      font-size: 1.8rem;
      font-weight: 400;
    }
    .user {
      display: flex;
      justify-content: flex-end;
      font-size: 1.2rem;
    }
    .word {
      margin-top: 10px;
      font-size: 1rem;
    }
  }
}

.talk-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 15px 15px;
  span > span {
    background: #f7f8fa;
    font-size: 14px;
    color: #999;
    height: 40px;
    line-height: 40px;
    padding-left: 10px;
    margin-bottom: 8px;
    overflow: hidden;
  }
}

.zc-top {
  display: flex;
  justify-content: space-between;
  align-items: center;

  > span {
    padding-right: 15px;
  }
}
.book {
  .right {
    display: flex;
    flex-direction: column;

    .bottom {
      display: flex;
      justify-content: space-between;

      .bottom-left {
        display: flex;
        justify-content: center;
        align-items: center;

        em {
          padding: 10px;
        }
      }
      .two {
        em {
          padding: 10px;
        }
      }
    }

    p {
      text-indent: 30px;
      padding: 10px 0;
    }

    span {
      display: flex;
      justify-content: flex-end;
      padding: 10px 0;
    }

    h3 {
      display: flex;
    }
  }
}
.list {
  // width: 1200px;

  // width: 100%;
  // display: flex;
  // margin: 10px;
  // width: 1200px;

  .right {
    display: flex;
    flex-direction: column;
    // margin: 10px;

    > span {
      margin-right: 15px;
    }
    .bottom {
      width: 100%;
      display: flex;
      border-bottom: 1px solid black;

      justify-content: space-between;
      .bottom-left {
        display: flex;
        justify-content: center;
        align-items: center;

        padding-left: 15px;
        em {
          padding: 0 15px;
        }
      }
      .two {
        em {
          padding: 15px;
        }
      }
    }

    p {
      line-height: 150%;
      text-indent: 30px;
      padding: 10px 0;
    }

    span {
      display: flex;
      justify-content: flex-end;
      // padding: 10px 0;
    }

    h3 {
      display: flex;
      justify-content: flex-end;
      padding: 10px 0;
    }
  }
}
</style>
